﻿using System;
using System.Collections.Generic;
using FinPlusComponents;
using QLNet;
using p = FinPlusAnalytics.QLConvParser;
using u = FinPlusUtility.Utility;

namespace FinPlusAnalytics
{
    public class BondCurve : FinPlusComponent
    {
        public string Name { get; private set; }
        public PiecewiseYieldCurve<Discount, LogLinear> PiecewiseBondCurve { get; private set; }
        public BondEngine BondEngine { get; private set; }

        //construct
        public BondCurve(string marketName, string name, string discountCurveName, DateTime settlementDate, string rates, string dayCount, double tolerance, string holidays = "")
        {
            Name = name;
            var market = Markets.Instance.GetMarket(marketName); 

            var res = u.StringToArray1d(rates, ',');
            var calendar = p.Calendar(holidays);
            var instruments = new List<RateHelper>();

            foreach (string r in res)
            {
                var rateHelper = market.GetRateHelper(r);
                if (rateHelper != null)
                    instruments.Add(rateHelper);
            }

            PiecewiseBondCurve = new PiecewiseYieldCurve<Discount, LogLinear>(calendar.adjust(settlementDate), instruments, p.DayCount(dayCount));
            market.SetCurve(name, PiecewiseBondCurve);
            BondEngine = new BondEngine(market, name, discountCurveName);
        }
    }
}
